package org.micro.alibaba.provider.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.micro.alibaba.common.domain.Item;
import org.micro.alibaba.provider.mapper.ItemMapper;
import org.springframework.stereotype.Service;

/**
 * @author keven
 * @date 2025-07-03 10:28 上午
 * @Description
 */
@Slf4j
@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> {


    public Item findById(Long id) {
        log.info("ItemService find item with id:{}", id);
        return baseMapper.selectById(id);
    }

    public void updateNum(Long id,Integer num) {
        Item item = findById(id);
        Integer resultNum = item.getNum() - num;
        if (resultNum<0) {
            throw new RuntimeException("库存不足");
        }
        item.setNum(resultNum);
        baseMapper.updateById(item);
    }

}
